package com.xzq.erp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.dto.WarehouseDTO;
import com.xzq.erp.domain.po.Product;
import com.xzq.erp.domain.po.ProductStock;
import com.xzq.erp.domain.po.Warehouse;
import com.xzq.erp.domain.query.WarehousePageQuery;
import com.xzq.erp.domain.vo.IdAndNameVO;
import com.xzq.erp.domain.vo.ProductStockListVO;
import com.xzq.erp.domain.vo.ProductStockVO;
import com.xzq.erp.domain.vo.WarehouseVO;
import com.xzq.erp.enums.ResponseCodeEnum;
import com.xzq.erp.exception.BizException;
import com.xzq.erp.mapper.ProductMapper;
import com.xzq.erp.mapper.ProductStockMapper;
import com.xzq.erp.mapper.WarehouseMapper;
import com.xzq.erp.service.IWarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库表 服务实现类
 * </p>
 *
 * @author xzq
 * @since 2025-05-26
 */
@Service
@RequiredArgsConstructor
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements IWarehouseService {

    private final ProductStockMapper productStockMapper;

    private final ProductMapper productMapper;

    @Override
    public List<IdAndNameVO> getWarehouseList() {
        List<Warehouse> list = lambdaQuery().list();
        return BeanUtil.copyToList(list, IdAndNameVO.class);
    }

    @Override
    public PageDTO<WarehouseVO> getWarehousePageList(WarehousePageQuery query) {
        PageHelper.startPage(query.getCurrent(),query.getSize());
        List<Warehouse> list = lambdaQuery()
                .like(StrUtil.isNotBlank(query.getName()), Warehouse::getName, query.getName())
                .like(StrUtil.isNotBlank(query.getCode()), Warehouse::getCode, query.getCode())
                .eq(Objects.nonNull(query.getType()), Warehouse::getType, query.getType())
                .eq(Objects.nonNull(query.getStatus()), Warehouse::getStatus, query.getStatus())
                .orderByDesc(Warehouse::getCreateTime)
                .list();
        PageInfo<Warehouse> page = new PageInfo<>(list);
        if(CollectionUtils.isEmpty(page.getList())){
            return PageDTO.empty(page.getTotal(),(long)page.getPages());
        }
        List<WarehouseVO> warehouseVOS = BeanUtil.copyToList(page.getList(), WarehouseVO.class);
        return new PageDTO<>(page.getTotal(),  (long)page.getPages(), warehouseVOS);
    }

    @Override
    public void addWarehouse(WarehouseDTO warehouseDTO) {
        Warehouse warehouse = BeanUtil.copyProperties(warehouseDTO, Warehouse.class);
        save(warehouse);
    }

    @Override
    public void updateWarehouse(WarehouseDTO warehouseDTO) {
        if(Objects.isNull(warehouseDTO.getId())){
            throw new BizException(ResponseCodeEnum.PARAM_NOT_VALID);
        }
        Warehouse warehouse = BeanUtil.copyProperties(warehouseDTO, Warehouse.class);
        updateById(warehouse);
    }

    @Override
    public WarehouseVO getWarehouseById(Long id) {
        Warehouse warehouse = getById(id);
        return BeanUtil.copyProperties(warehouse, WarehouseVO.class);
    }

    @Override
    public List<ProductStockVO> getWarehouseStock(Long warehouseId) {
        LambdaQueryWrapper<ProductStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductStock::getWarehouseId, warehouseId);
        List<ProductStock> list = productStockMapper.selectList(queryWrapper);
        List<Long> productIds = list.stream().map(ProductStock::getProductId).toList();
        if(CollectionUtils.isEmpty(list) || CollectionUtils.isEmpty(productIds)){
            return List.of();
        }
        List<Product> products = productMapper.selectBatchIds(productIds);
        Map<Long, Product> productMap = products.stream()
                .collect(Collectors.toMap(Product::getId, product -> product));
        List<ProductStockVO> vos = BeanUtil.copyToList(list, ProductStockVO.class);
        for(ProductStockVO vo : vos){
            Product product = productMap.get(vo.getProductId());
            if(Objects.isNull(product)){
                continue;
            }
            vo.setProductName(product.getName());
        }
        return vos;
    }
}
